const CryptoSecret = "__CryptoJS_Secret__"
import CryptoJS from "crypto-js"

/**
 * 加密数据
 * @param data - 数据
 */
export function encrypt(data: unknown) {
	const newData = JSON.stringify(data)
	return CryptoJS.AES.encrypt(newData, CryptoSecret).toString()
}

/**
 * 解密数据
 * @param cipherText - 密文
 */
export function decrypt(cipherText: string) {
	const bytes = CryptoJS.AES.decrypt(cipherText, CryptoSecret)
	const originalText = bytes.toString(CryptoJS.enc.Utf8)
	if (originalText) {
		return JSON.parse(originalText)
	}
	return null
}

/**Md5加密*/
export function md5ToString(str: string) {
	return CryptoJS.MD5(str).toString()
}
